home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pine / pine3.07 / pico / search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-11  |  10.8 KB  |  410 lines

  1. /*
  2.  * Program:    Searching routines
  3.  *
  4.  * Modifier:    Michael Seibel
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: mikes@cac.washington.edu
  11.  *
  12.  * Date:    19 Jan 1991
  13.  * Last Edited:    6 Jan 1992
  14.  *
  15.  * Copyright 1991 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notice appears in all copies and that both the
  20.  * above copyright notice and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  30.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  32.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35. /*
  36.  * The functions in this file implement commands that search in the forward
  37.  * and backward directions. There are no special characters in the search
  38.  * strings. Probably should have a regular expression search, or something
  39.  * like that.
  40.  *
  41.  */
  42.  
  43. #include        <stdio.h>
  44. #include    "osdep.h"
  45. #include    "estruct.h"
  46. #include    "pico.h"
  47. #include        "edef.h"
  48.  
  49.  
  50. /*
  51.  * Search forward. Get a search string from the user, and search, beginning at
  52.  * ".", for the string. If found, reset the "." to be just after the match
  53.  * string, and [perhaps] repaint the display. Bound to "C-S".
  54.  */
  55.  
  56. /*    string search input parameters    */
  57.  
  58. #define    PTBEG    1    /* leave the point at the begining on search */
  59. #define    PTEND    2    /* leave the point at the end on search */
  60.  
  61.  
  62.  
  63.  
  64. static char *SearchHelpText[] = {
  65. "Help for Search Command",
  66. " ",
  67. "\tEnter the words or characters you would like to search",
  68. "~\tfor, then press ~R~e~t~u~r~n.  The search then takes place.",
  69. "\tWhen the characters or words that you entered ",
  70. "\tare found, the buffer will be redisplayed with the cursor ",
  71. "\tat the beginning of the selected text.",
  72. " ",
  73. "\tThe most recent string for which a search was made is",
  74. "\tdisplayed in the \"Search\" prompt between the square",
  75. "\tbrackets.  This string is the default search prompt.",
  76. "~        Hitting only ~R~e~t~u~r~n or at the prompt will cause the",
  77. "\tsearch to be made with the default value.",
  78. "  ",
  79. "\tThe text search is not case sensitive, and will examine the",
  80. "\tentire message.",
  81. "  ",
  82. "\tShould the search fail, a message will be displayed.",
  83. "  ",
  84. "End of Search Help.",
  85. "  ",
  86. NULL
  87. };
  88.  
  89.  
  90. /*
  91.  * Compare two characters. The "bc" comes from the buffer. It has it's case
  92.  * folded out. The "pc" is from the pattern.
  93.  */
  94. eq(bc, pc)
  95. int bc;
  96. int pc;
  97. {
  98.     if ((curwp->w_bufp->b_mode & MDEXACT) == 0){
  99.     if (bc>='a' && bc<='z')
  100.       bc -= 0x20;
  101.  
  102.     if (pc>='a' && pc<='z')
  103.       pc -= 0x20;
  104.     }
  105.  
  106.     return(bc == pc);
  107. }
  108.  
  109.  
  110. forwsearch(f, n)
  111. {
  112.     register int status;
  113.     int wrapt = FALSE;
  114.     char show[512];
  115.  
  116.     /* resolve the repeat count */
  117.     if (n == 0)
  118.       n = 1;
  119.  
  120.     if (n < 1)            /* search backwards */
  121.       return(0);
  122.  
  123.     /* ask the user for the text of a pattern */
  124.     while(1){
  125.     wkeyhelp("GC0000000000","Get Help,Cancel");
  126.     if(Pmaster == NULL)
  127.       sgarbk = TRUE;
  128.  
  129.     if ((status = readpattern("Search")) == TRUE){
  130.         break;
  131.     }
  132.     else{
  133.         switch(status){
  134.           case HELPCH:            /* help requested */
  135.         if(Pmaster)
  136.           (*Pmaster->helper)(Pmaster->search_help,
  137.                      "Help for Searching", 1);
  138.         else
  139.           pico_help(SearchHelpText, "Help for Searching", 1);
  140.           case (CTRL|'L'):            /* redraw requested */
  141.         refresh(FALSE, 1);
  142.         update();
  143.         break;
  144.           default:
  145.         curwp->w_flag |= WFMODE;
  146.         if(status == ABORT)
  147.           emlwrite("Search Aborted", NULL);
  148.         else
  149.           mlerase();
  150.         return(FALSE);
  151.         }
  152.     }
  153.     }
  154.  
  155.     curwp->w_flag |= WFMODE;
  156.  
  157.     /*
  158.      * This was added late in the game and is kind of a hack.
  159.      * What is wanted is an easy way to move immediately to the top or
  160.      * bottom of the buffer.  This makes it not-too-obvious, but saves
  161.      * key commands.
  162.      */
  163.     if(pat[0] == '\\'){
  164.     if(!strcmp(&pat[1], "top")){
  165.         gotobob(0, 1);
  166.         mlerase();
  167.         return(1);
  168.     }
  169.  
  170.     if(!strcmp(&pat[1], "bottom")){
  171.         gotoeob(0, 1);
  172.         mlerase();
  173.         return(1);
  174.     }
  175.     }
  176.  
  177.     /*
  178.      * This code is kind of dumb.  What I want is successive C-W 's to 
  179.      * move dot to successive occurences of the pattern.  So, if dot is
  180.      * already sitting at the beginning of the pattern, then we'll move
  181.      * forward a char before beginning the search.  We'll let the
  182.      * automatic wrapping handle putting the dot back in the right 
  183.      * place...
  184.      */
  185.     status = 0;        /* using "status" as int temporarily! */
  186.     while(1){
  187.     if(pat[status] == '\0'){
  188.         forwchar(0, 1);
  189.         break;        /* find next occurance! */
  190.     }
  191.  
  192.     if(status + curwp->w_doto >= llength(curwp->w_dotp) ||
  193.        !eq(pat[status],lgetc(curwp->w_dotp, curwp->w_doto + status)))
  194.       break;        /* do nothing! */
  195.     status++;
  196.     }
  197.  
  198.     /* search for the pattern */
  199.     
  200.     while (n-- > 0) {
  201.     if((status = forscan(&wrapt,&pat[0],PTBEG)) == FALSE)
  202.       break;
  203.     }
  204.  
  205.     /* and complain if not there */
  206.     if (status == FALSE){
  207.     expandp(&pat[0], &show[0], 50);
  208.     emlwrite("\"%s\" not found", show);
  209.     } 
  210.     else if(wrapt == TRUE){
  211.     emlwrite("Search Wrapped");
  212.     }
  213.     else if(status == TRUE)
  214.       emlwrite("");
  215.  
  216.     return(status);
  217. }
  218.  
  219.  
  220. /*
  221.  * Read a pattern. Stash it in the external variable "pat". The "pat" is not
  222.  * updated if the user types in an empty line. If the user typed an empty line,
  223.  * and there is no old pattern, it is an error. Display the old pattern, in the
  224.  * style of Jeff Lomicka. There is some do-it-yourself control expansion.
  225.  * change to using <ESC> to delemit the end-of-pattern to allow <NL>s in
  226.  * the search string.
  227.  */
  228. readpattern(prompt)
  229. char *prompt;
  230. {
  231.     register int s;
  232.     char tpat[NPAT+20];
  233.  
  234.     strcpy(tpat, prompt);    /* copy prompt to output string */
  235.         if(pat[0] != '\0'){
  236.         strcat(tpat, " [");    /* build new prompt string */
  237.         expandp(&pat[0], &tpat[strlen(tpat)], NPAT/2);    /* add old pattern */
  238.         strcat(tpat, "]");
  239.         }
  240.     strcat(tpat, " : ");
  241.  
  242.     s = mlreplyd(tpat, tpat, NPAT, QNORML);     /* Read pattern */
  243.  
  244.     if (s == TRUE)                /* Specified */
  245.         strcpy(pat, tpat);
  246.     else if (s == FALSE && pat[0] != 0)    /* CR, but old one */
  247.         s = TRUE;
  248.  
  249.     return(s);
  250. }
  251.  
  252.  
  253. forscan(wrapt,patrn,leavep)    /*    search forward for a <patrn>    */
  254. int    *wrapt;        /* boolean indicating search wrapped */
  255. char *patrn;        /* string to scan for */
  256. int leavep;        /* place to leave point
  257.                 PTBEG = begining of match
  258.                 PTEND = at end of match        */
  259.  
  260. {
  261.     register LINE *curline;        /* current line during scan */
  262.     register int curoff;        /* position within current line */
  263.     register LINE *lastline;    /* last line position during scan */
  264.     register int lastoff;        /* position within last line */
  265.     register int c;            /* character at current position */
  266.     register LINE *matchline;    /* current line during matching */
  267.     register int matchoff;        /* position in matching line */
  268.     register char *patptr;        /* pointer into pattern */
  269.     register int stopoff;        /* offset to stop search */
  270.     register LINE *stopline;    /* line to stop search */
  271.  
  272.     *wrapt = FALSE;
  273.  
  274.     /*
  275.      * the idea is to set the character to end the search at the 
  276.      * next character in the buffer.  thus, let the search wrap
  277.      * completely around the buffer.
  278.      * 
  279.      * first, test to see if we are at the end of the line, 
  280.      * otherwise start searching on the next character. 
  281.      */
  282.     if(curwp->w_doto == llength(curwp->w_dotp)){
  283.         /*
  284.          * dot is not on end of a line
  285.          * start at 0 offset of the next line
  286.          */
  287.         stopoff = curoff  = 0;
  288.         stopline = curline = lforw(curwp->w_dotp);
  289.         if (curwp->w_dotp == curbp->b_linep)
  290.           *wrapt = TRUE;
  291.     }
  292.     else{
  293.         stopoff = curoff  = curwp->w_doto;
  294.         stopline = curline = curwp->w_dotp;
  295.     }
  296.  
  297.     /* scan each character until we hit the head link record */
  298.  
  299.     /*
  300.      * maybe wrapping is a good idea
  301.      */
  302.     while (curline){
  303.  
  304.         if (curline == curbp->b_linep)
  305.             *wrapt = TRUE;
  306.  
  307.         /* save the current position in case we need to
  308.            restore it on a match            */
  309.  
  310.         lastline = curline;
  311.         lastoff = curoff;
  312.  
  313.         /* get the current character resolving EOLs */
  314.  
  315.         if (curoff == llength(curline)) {    /* if at EOL */
  316.             curline = lforw(curline);    /* skip to next line */
  317.             curoff = 0;
  318.             c = '\n';            /* and return a <NL> */
  319.         } else
  320.             c = lgetc(curline, curoff++);    /* get the char */
  321.  
  322.         /* test it against first char in pattern */
  323.         if (eq(c, patrn[0]) != FALSE) {    /* if we find it..*/
  324.             /* setup match pointers */
  325.             matchline = curline;
  326.             matchoff = curoff;
  327.             patptr = &patrn[0];
  328.  
  329.             /* scan through patrn for a match */
  330.             while (*++patptr != 0) {
  331.                 /* advance all the pointers */
  332.                 if (matchoff == llength(matchline)) {
  333.                     /* advance past EOL */
  334.                     matchline = lforw(matchline);
  335.                     matchoff = 0;
  336.                     c = '\n';
  337.                 } else
  338.                     c = lgetc(matchline, matchoff++);
  339.  
  340.                 /* and test it against the pattern */
  341.                 if (eq(*patptr, c) == FALSE)
  342.                     goto fail;
  343.             }
  344.  
  345.             /* A SUCCESSFULL MATCH!!! */
  346.             /* reset the global "." pointers */
  347.             if (leavep == PTEND) {    /* at end of string */
  348.                 curwp->w_dotp = matchline;
  349.                 curwp->w_doto = matchoff;
  350.             } else {        /* at begining of string */
  351.                 curwp->w_dotp = lastline;
  352.                 curwp->w_doto = lastoff;
  353.             }
  354.             curwp->w_flag |= WFMOVE; /* flag that we have moved */
  355.             return(TRUE);
  356.  
  357.         }
  358. fail:;            /* continue to search */
  359.         if((curline == stopline) && (curoff == stopoff))
  360.             break;            /* searched everywhere... */
  361.     }
  362.     /* we could not find a match */
  363.  
  364.     return(FALSE);
  365. }
  366.  
  367. /*     expandp:    expand control key sequences for output        */
  368.  
  369. expandp(srcstr, deststr, maxlength)
  370.  
  371. char *srcstr;    /* string to expand */
  372. char *deststr;    /* destination of expanded string */
  373. int maxlength;    /* maximum chars in destination */
  374.  
  375. {
  376.     char c;        /* current char to translate */
  377.  
  378.     /* scan through the string */
  379.     while ((c = *srcstr++) != 0) {
  380.         if (c == '\n') {        /* its an EOL */
  381.             *deststr++ = '<';
  382.             *deststr++ = 'N';
  383.             *deststr++ = 'L';
  384.             *deststr++ = '>';
  385.             maxlength -= 4;
  386.         } else if (c < 0x20 || c == 0x7f) {    /* control character */
  387.             *deststr++ = '^';
  388.             *deststr++ = c ^ 0x40;
  389.             maxlength -= 2;
  390.         } else if (c == '%') {
  391.             *deststr++ = '%';
  392.             *deststr++ = '%';
  393.             maxlength -= 2;
  394.  
  395.         } else {            /* any other character */
  396.             *deststr++ = c;
  397.             maxlength--;
  398.         }
  399.  
  400.         /* check for maxlength */
  401.         if (maxlength < 4) {
  402.             *deststr++ = '$';
  403.             *deststr = '\0';
  404.             return(FALSE);
  405.         }
  406.     }
  407.     *deststr = '\0';
  408.     return(TRUE);
  409. }
  410.